<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Kupai
 *
 * @author dragon.hu add @2016.2.23
 */
require_once 'BaseChannel.php';

//酷派
class Kupai implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDSqR6P/FfyHhzATJqIiVzjP0QmzgZi4A/Huwc5BWZei4RGqjYUSdNJ79sV9L/QGfzVEq8uQoFuIQVhC97wusVeRiQ2r9eh1KOzrFUqcjnuLYSpTsOIRRZQSmUoq/5tdAdc2S6l4mnDTBItHhLHHCWptvt1F97c5gd4/CZ95s7yCQIDAQAB"; //公钥
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("kupai", $_REQUEST);
		
		$notifyData = file_get_contents("php://input");
		$transdata = array();
		$result = $this->parseResp($notifyData, $paymentKey, $transdata);
		
		if($result) 
		{ //验证通过
			 $status = STATUS_SUCCESS;
			 if ($transdata['result'] != 0)
			 {
			 	$status = STATUS_FAILD;
			 }
			//进行发货
			if(false == $orderLog->recordOrder($status, $transdata['money'], $transdata['cporderid'], "kupai", $platform,$transdata['transid']))
			{
				$ret="FAILURE";
				$orderLog->log("kupai",array("order"=>$transdata['cporderid'],"money"=>$transdata['money'],"serverresult"=>"faild"));
			}else 
			{
				//支付成功
				$ret="SUCCESS";
				$orderLog->log("kupai",array("order"=>$transdata['cporderid'],"money"=>$transdata['money'],"serverresult"=>"success"));
			}
			 
		} 
		else 
		{
			$ret = "FAILURE";
			$orderLog->log("kupai","validsign error");
		}
		return $ret;
	}
	/**************************************酷派提供的接口*****************************************/
	/**
	 * 解析response报文
	 * $content  收到的response报文
	 * $pkey     爱贝平台公钥，用于验签
	 * $respJson 返回解析后的json报文
	 * return    解析成功TRUE，失败FALSE
	 */
	function parseResp($content, $pkey, &$respJson) {
		$arr=array_map(create_function('$v', 'return explode("=", $v);'), explode('&', $content));
		foreach($arr as $value) {
			$resp[($value[0])] = urldecode($value[1]);
		}
	
		//解析transdata
		if(array_key_exists("transdata", $resp)) {
			$respJson = json_decode($resp["transdata"], true);  //直接改成数组的形式
		} else {
			return FALSE;
		}
	
		//验证签名，失败应答报文没有sign，跳过验签
		if(array_key_exists("sign", $resp)) {
			//校验签名
			$pkey = $this->formatPubKey($pkey);
			return $this->verify($resp["transdata"], $resp["sign"], $pkey);
		} else if(!array_key_exists("errmsg", $respJson)) {
			return FALSE;
		}
	
		return TRUE;
	}
	/**格式化公钥
	 * $pubKey PKCS#1格式的公钥串
	 * return pem格式公钥， 可以保存为.pem文件
	 */
	function formatPubKey($pubKey) {
		$fKey = "-----BEGIN PUBLIC KEY-----\n";
		$len = strlen($pubKey);
		for($i = 0; $i < $len; ) {
			$fKey = $fKey . substr($pubKey, $i, 64) . "\n";
			$i += 64;
		}
		$fKey .= "-----END PUBLIC KEY-----";
		return $fKey;
	}
	/**RSA验签
	 * $data待签名数据
	 * $sign需要验签的签名
	 * $pubKey爱贝公钥
	 * 验签用爱贝公钥，摘要算法为MD5
	 * return 验签是否通过 bool值
	 */
	function verify($data, $sign, $pubKey)  {
		//转换为openssl格式密钥
		$res = openssl_get_publickey($pubKey);
	
		//调用openssl内置方法验签，返回bool值
		$result = (bool)openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_MD5);
	
		//释放资源
		openssl_free_key($res);
	
		//返回资源是否成功
		return $result;
	}
	/***************************************************************************************/
}